﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "My2dgame/Public/WarriorCharacter.h"
#include "PaperFlipbookComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "Camera/CameraComponent.h"
#include "Kismet/GameplayStatics.h"


AWarriorCharacter::AWarriorCharacter()
{
	//设置精灵角色
	GetSprite()->SetCastShadow(true);//启用阴影
	GetSprite()->SetRelativeLocation(FVector(22.0f, 0.0f, 18.0f));
	GetSprite()->SetRelativeScale3D(FVector(5.0f));
	//pawn中使用控制器选择，因为Character本身就继承自pawn类
	bUseControllerRotationYaw = false;
	//生成相机摇臂
	SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	SpringArmComponent->SetupAttachment(RootComponent);
	SpringArmComponent->TargetArmLength = 900.0f;
	SpringArmComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f));
	SpringArmComponent->SetRelativeRotation(FRotator(0.0f, -90.0f, 0.0f));
	SpringArmComponent->bInheritYaw=false;
	//相机摇臂旋转 z轴对应Yaw x轴对应Pitch y轴对应Roll
	SpringArmComponent->SetRelativeRotation(FRotator(0.0f, -90.0f, -0.0f));
	SpringArmComponent->bEnableCameraLag = true;//相机是否有滞后效果
	//生成相机
	CameraComponent= CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	CameraComponent->SetupAttachment(SpringArmComponent);
	CameraComponent->bUsePawnControlRotation = false;
	//设置运动组件
	GetCharacterMovement()->bOrientRotationToMovement=true;//随着移动选择
	GetCharacterMovement()->RotationRate = FRotator(0.0f, 10000.0f, 0.0f);//设置选择速率
}

bool AWarriorCharacter::IsLanding() const
{
	return IsGround() && MoveInputValue==0.f;
}

bool AWarriorCharacter::IsGround() const
{
	return GetCharacterMovement()->IsMovingOnGround();
}

void AWarriorCharacter::SetMoveInputValue(const float InpuntValue)
{
	MoveInputValue=InpuntValue;
}

void AWarriorCharacter::BeginPlay()
{
	Super::BeginPlay();

	//添加落地委托
	LandedDelegate.AddDynamic(this, &AWarriorCharacter::OnCharacterLanded);

	//绑定增强输入的值
	check(AuraContext);
	//获取玩家控制器
	APlayerController* PlayerController = Cast<APlayerController>(GetController());
	if (PlayerController)
	{
		//获取输入组件
		ULocalPlayer* LocalPlayer = PlayerController->GetLocalPlayer();
		check(LocalPlayer);
		
		UEnhancedInputLocalPlayerSubsystem* Subsystem = LocalPlayer->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>();
		if (Subsystem)
		{
			Subsystem->AddMappingContext(AuraContext, 0);
		}
	}
}

void AWarriorCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	//获取增强输入
	UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);
	EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AWarriorCharacter::Move);
	EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Completed, this, &AWarriorCharacter::SetMoveInputValue,0.f);
	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &AWarriorCharacter::Jump);
	EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &AWarriorCharacter::StopJumping);
}

void AWarriorCharacter::Move(const FInputActionValue& InputActionValue)
{
	//添加左右移动
	float MovementVector = InputActionValue.Get<float>();
	if (MovementVector != 0)
	{
		//移动角色
		AddMovementInput(FVector::ForwardVector, MovementVector);
		SetMoveInputValue(MovementVector);
	}
}

void AWarriorCharacter::Jump()
{
	Super::Jump();
	IsJumped = true;
}

void AWarriorCharacter::OnCharacterLanded(const FHitResult& Hit)
{
	IsJumped = false;
}

